package com.example.musicplayer.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.musicplayer.R;
import com.example.musicplayer.adapter.UserManageAdapter;
import com.example.musicplayer.user.UserManage;

import java.util.List;

public class UserManageActivity extends BaseSettingsActivity implements View.OnClickListener, UserManageAdapter.Callback {
    private ListView mListView;
    private UserManageAdapter mAdapter;
    private View mBottomView;
    private View mFooterView;

    @SuppressLint("InflateParams")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setEnableMusicTabView(false);
        mListView = (ListView) LayoutInflater.from(this).inflate(R.layout.list_view, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , 0);
        lp.weight = 1;
        ((LinearLayout) findViewById(R.id.linear_container)).addView(mListView, lp);
        mAdapter = new UserManageAdapter(this, UserManageAdapter.MODE_NORMAL, mListView);
        mListView.setAdapter(mAdapter);
        mAdapter.setCallback(this);
        //add footer view
        mFooterView = LayoutInflater.from(this).inflate(R.layout.empty_view, null);
        mFooterView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , (int) getResources().getDimension(R.dimen.user_manage_bottom_view_height)));
        mFooterView.setEnabled(false);
        mListView.addFooterView(mFooterView);
        mListView.setVerticalScrollBarEnabled(false);
        mListView.setFooterDividersEnabled(false);
        //init confirm area
        mBottomView = LayoutInflater.from(this).inflate(R.layout.user_manage_bottom_view, null);
        lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , (int) getResources().getDimension(R.dimen.user_manage_bottom_view_height));
        ((LinearLayout) findViewById(R.id.linear_container)).addView(mBottomView, lp);
        mBottomView.setVisibility(mAdapter.getMode() == UserManageAdapter.MODE_SELECT ? View.VISIBLE : View.GONE);
        //toolbar text
        findViewById(R.id.toolbar_text_right).setOnClickListener(this);
        findViewById(R.id.toolbar_text_right_second).setOnClickListener(this);
        updateToolbarText();
    }

    @Override
    protected int setActivityTitle() {
        return R.string.title_user_manage_activity;
    }

    private void updateToolbarText() {
        TextView multiSelect = findViewById(R.id.toolbar_text_right);
        TextView selectAll = findViewById(R.id.toolbar_text_right_second);
        if (mAdapter.getMode() == UserManageAdapter.MODE_NORMAL) {
            multiSelect.setText(R.string.user_manage_multi_select);
            selectAll.setVisibility(View.GONE);
        } else {
            multiSelect.setText(R.string.cancel);
            selectAll.setVisibility(View.VISIBLE);
            if (mAdapter.isAllSelected()) {
                selectAll.setText(R.string.user_manage_deselect_all);
            } else {
                selectAll.setText(R.string.user_manage_select_all);
            }
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_text_right:
                if (mAdapter.getMode() == UserManageAdapter.MODE_NORMAL) {
                    mAdapter.setMode(UserManageAdapter.MODE_SELECT);
                    mBottomView.setVisibility(View.VISIBLE);
                    mListView.removeFooterView(mFooterView);
                } else {
                    mAdapter.setMode(UserManageAdapter.MODE_NORMAL);
                    mBottomView.setVisibility(View.GONE);
                    mListView.addFooterView(mFooterView);
                }
                updateToolbarText();
                break;
            case R.id.toolbar_text_right_second:
                TextView selectAll = findViewById(R.id.toolbar_text_right_second);
                mAdapter.selectAll(getString(R.string.user_manage_select_all).equals(selectAll.getText().toString()));
                updateToolbarText();
                break;
            case R.id.delete:
                mAdapter.deleteSelectedUser();
                break;
            case R.id.cancel:
                findViewById(R.id.toolbar_text_right).performClick();
                break;
        }
    }

    @Override
    public void onItemSelectedStateChanged(int position, boolean isSelected) {
        updateToolbarText();
    }

    @Override
    public void onDeleteUser(final List<Integer> userIdList) {
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < userIdList.size(); i++) {
                    UserManage.deleteUserAndData(userIdList.get(i));
                }
            }
        }.start();
        updateToolbarText();
    }
}